<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增项目工时')" />
    <th:block th:include="include :: datetimepicker-css" />
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content" id="cxSelect">
        <form class="form-horizontal m" id="form-projectHour-add">
            <input type="hidden" name="userId" th:value="${userId}" />
            <input type="hidden" name="type" value="1" th:if="${type == 'normal'}">
            <input type="hidden" name="type" value="2" th:if="${type=='maintenance'}">
            <div class="form-group">    
                <label class=" col-sm-3 control-label is-required">项目：</label>
                <div class="col-sm-8">
                    <select name="projectId" id="projectId" class="form-control project" required>
                        <option value="">请选择</option>
                    </select>
                </div>
            </div>

            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">工时模块：</label>
                <div class="col-sm-8">
                    <div class="input-group">
                        <select name="moduleId" id="moduleId" class="form-control module" required>
                            <option value="">请选择</option>
                        </select>
                        <span class="input-group-btn">
                                <button type="button" class="btn btn-primary" onclick="addModule()">添加</button>
                        </span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">工时：</label>
                <div class="col-sm-8">
                    <input name="workHour" class="form-control" type="text" required>
                </div>
            </div>
            <div class="form-group">    
                <label class="col-sm-3 control-label is-required">工时日期：</label>
                <div class="col-sm-8">
                    <div class="input-group date">
                        <input name="workDate"  th:value="${#dates.format(new java.util.Date(), 'yyyy-MM-dd')}" class="form-control" placeholder="yyyy-MM-dd" type="text" required>

                        <span class="input-group-addon"><i class="fa fa-calendar"></i></span>
                    </div>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label is-required">工时内容：</label>
                <div class="col-sm-8">
                    <textarea name="description" class="form-control" rows="8" required></textarea>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: datetimepicker-js" />
    <th:block th:include="include :: jquery-cxselect-js" />
    <script th:inline="javascript">
        var prefix = ctx + "erp/projectHour"
        $("#form-projectHour-add").validate({
            focusCleanup: true
        });

        function submitHandler() {
            if ($.validate.form()) {
                $.operate.save(prefix + "/add", $('#form-projectHour-add').serialize());
            }
        }

        $("input[name='workDate']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        $("input[name='createAt']").datetimepicker({
            format: "yyyy-mm-dd",
            minView: "month",
            autoclose: true
        });

        // 通过默认url获取
        $.cxSelect.defaults.url = '/erp/projectHour/module/myProjectModules/' + [[${type}]];
        $('#cxSelect').cxSelect({
            selects: ['project', 'module'],
            jsonValue: 'value',
            jsonName: 'name',
            jsonSub: 'selects',
            required: true,
            nodata: 'none'
        });

        function addModule()
        {
            var projectId = $('#projectId').val();
            layer.prompt({title: '请输入模块名称', formType: 2}, function(pass, index){
                $.operate.post('/erp/projectHour/module/add',{"name":pass, "projectId": projectId},function(data){
                   if(data.code == '0'){
                       layer.close(index);
                       $('#moduleId').append('<option value="' + data.data.id+ '" selected="selected">' + pass + '</option>');
                   }
                });
            });
        }
    </script>
</body>
</html>